package J1202_night;

import java.util.Stack;

//用栈来实现队列
public class Stack_to_Queue {
    private Stack<Integer> s1;
    private Stack<Integer> s2;
    public Stack_to_Queue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }
    //“入栈”都放到第一个队列中
    //“出栈”出s2这个栈 如果s2为空 就把所有元素放到s2里面
    //如果两个栈都为空 就说明队列为空
    public void push(int x) {
        s1.push(x);
    }
    public int pop() {
        if(empty()) {
            return -1;
        }
        if(s2.isEmpty()) {
            while(!s1.isEmpty()) {
                s2.push(s1.pop());
            }
        }
        return s2.pop();
    }
    public int peek() {
        if(empty()) {
            return -1;
        }
        if(s2.isEmpty()) {
            while(!s1.empty()) {
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }
    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }
}
